package com.sqx.modules.disease.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.disease.dao.DiagnosticDao;
import com.sqx.modules.disease.entity.Diagnostic;
import com.sqx.modules.disease.entity.Prescript;
import com.sqx.modules.disease.entity.PrescriptMaterials;
import com.sqx.modules.disease.service.DiagnosticService;
import com.sqx.modules.disease.service.PrescriptMaterialsService;
import com.sqx.modules.disease.service.PrescriptService;
import com.sqx.modules.disease.utils.ChangLiangChi;
import com.sqx.modules.disease.utils.ResponseData;
import com.sqx.modules.disease.utils.SequenceUtils;
import com.sqx.modules.disease.vo.DiagnosticVo;
import com.sqx.modules.disease.vo.PrescriptVO;
import com.sqx.modules.health.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 诊疗配置表(Diagnostic)表服务实现类
 *
 * @author makejava
 * @since 2023-11-20 21:53:22
 */
@Service("diagnosticService")
public class DiagnosticServiceImpl extends ServiceImpl<DiagnosticDao, Diagnostic> implements DiagnosticService {

    @Resource
    PrescriptMaterialsService prescriptMaterialsService;
    @Resource
    PrescriptService prescriptService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData saveData(DiagnosticVo diagnosticVo) {
        Diagnostic diagnostic = new Diagnostic();
        BeanUtils.copyProperties(diagnosticVo, diagnostic);
        diagnostic.setId(SequenceUtils.nextId());
        String diagnoedId = diagnosticVo.getDiagnoedName();
        String tongueId = diagnosticVo.getTongueName();
        String smellId = diagnosticVo.getSmellName();
        String surId = diagnosticVo.getSurName();
        String pulseId = diagnosticVo.getPulseName();
        if (StringUtil.isEmpty(diagnosticVo.getDiseaseName()) && null == diagnosticVo.getDiseaseId()) {
            return new ResponseData("-1", "病症名称为必填！！");
        }
        if (StringUtil.isEmpty(diagnosticVo.getYinyangName()) && null == diagnosticVo.getYinyangId()) {
            return new ResponseData("-1", "阴阳病症为必填！！");
        }
        if (StringUtil.isEmpty(diagnosticVo.getVisceraName()) && null == diagnosticVo.getVisceraId()) {
            return new ResponseData("-1", "脏腑病症为必填！！");
        }
        if (StringUtil.isEmpty(diagnosticVo.getPlanName())) {
            return new ResponseData("-1", "治则为必填！！");
        } else {
            diagnosticVo.setPlanId(SequenceUtils.nextId());

        }
        if (StringUtil.isEmpty(diagnosticVo.getPrescriptName())) {
            return new ResponseData("-1", "药方名称为必填！！");
        } else {
            diagnostic.setPrescriptId(SequenceUtils.nextId());
            Prescript prescript = new Prescript();
            prescript.setCreateTime(new Date());
            prescript.setUpdateTime(new Date());
            prescript.setPrescriptId(diagnostic.getPrescriptId());
            prescript.setPrescriptType1(diagnosticVo.getYinyangName());
            prescript.setPrescriptType2(diagnosticVo.getVisceraName());
            prescript.setRemark(diagnosticVo.getRemark());
            prescript.setUsage(diagnosticVo.getUsage());
            prescript.setEfficacy(diagnosticVo.getPlanName());
            prescript.setPrescriptName(diagnosticVo.getPrescriptName());
            prescriptService.save(prescript);
        }
        if (null == diagnosticVo.getPrescriptVOList() && diagnosticVo.getPrescriptVOList().size() > 0) {
            return new ResponseData("-1", "请填写药材！！");
        } else {
            List<PrescriptVO> prescriptVOList = diagnosticVo.getPrescriptVOList();
            List<PrescriptMaterials> list = new ArrayList<>();
            for (PrescriptVO prescriptVO : prescriptVOList) {
                if (StringUtil.isEmpty(prescriptVO.getMaterialsName()) | StringUtil.isEmpty(prescriptVO.getDosage())) {
                    return new ResponseData("-1", "请填写完整药材或者剂量！！");
                } else {
                    PrescriptMaterials prescriptMaterials = new PrescriptMaterials();
                    prescriptMaterials.setCreateTime(new Date());
                    prescriptMaterials.setUpdateTime(new Date());
                    prescriptMaterials.setPrescriptId(diagnostic.getPrescriptId());
                    prescriptMaterials.setMaterialsId(SequenceUtils.nextId());
                    prescriptMaterials.setPrescriptType1(diagnosticVo.getYinyangName());
                    prescriptMaterials.setPrescriptType2(diagnosticVo.getVisceraName());
                    prescriptMaterials.setRemark(diagnosticVo.getRemark());
                    prescriptMaterials.setDosage(prescriptVO.getDosage());
                    prescriptMaterials.setMaterialsName(prescriptVO.getMaterialsName());
                    list.add(prescriptMaterials);
                }
            }
            prescriptMaterialsService.saveBatch(list);
        }
        //问诊
        if (StringUtil.isNotEmpty(diagnoedId)) {
            diagnostic.setDiagnoedId(SequenceUtils.nextId());
            diagnostic.setDiagnoedName(getDataStr(diagnoedId, ChangLiangChi.DIAGNOED_MAP));
        } else {
            return new ResponseData("-1", "请选择问诊列！！");
        }
        //舌诊
        if (StringUtil.isNotEmpty(tongueId)) {
            diagnostic.setTongueId(SequenceUtils.nextId());
            diagnostic.setTongueName(getDataStr(tongueId, ChangLiangChi.TONGUE_MAP));
        }
        //闻诊
        if (StringUtil.isNotEmpty(smellId)) {
            diagnostic.setSmellId(SequenceUtils.nextId());
            diagnostic.setSmellName(getDataStr(smellId, ChangLiangChi.SMELL_MAP));
        }
        //面诊
        if (StringUtil.isNotEmpty(surId)) {
            diagnostic.setSurId(SequenceUtils.nextId());
            diagnostic.setSurName(getDataStr(surId, ChangLiangChi.SUR_MAP));
        }
        //脉诊
        if (StringUtil.isNotEmpty(pulseId)) {
            diagnostic.setPlanId(SequenceUtils.nextId());
            diagnostic.setPlanName(getDataStr(pulseId, ChangLiangChi.PULE_MAP));
        }
        this.save(diagnostic);
        return new ResponseData("0", "添加成功！");
    }

    private String getDataStr(String strId, Map<String, String> diagnoedMap) {
        List<String> list = Arrays.asList(strId.split(";"));
        List<String> stringList = new ArrayList<>();
        for (String s : list) {
            stringList.add(diagnoedMap.get(s));
        }
        return String.join(";", stringList);
    }
}

